package com.jse.oo.design.pattern.behavioral.observer;

import java.util.ArrayList;
import java.util.List;

public class Subject {
	private List<Obsever> obsevers;
	private boolean state;
	
	public Subject() {
		obsevers = new ArrayList<>();
	}
	
	public void addObsever(Obsever obsever) {
		obsevers.add(obsever);
	}
	
	public void deleteObsever(Obsever obsever) {
		obsevers.remove(obsever);
	}
	
	public void deleteAllObsevers() {
		for (Obsever obsever : obsevers) {
			obsevers.remove(obsever);
		}
	}
	
	public List<Obsever> getObsevers() {
		return obsevers;
	}

	public void setObsevers(List<Obsever> obsevers) {
		this.obsevers = obsevers;
	}

	public boolean isState() {
		return state;
	}

	public void setState(boolean state) {
		this.state = state;
	}

	public void notifyObservers() {
		if (state) {
			if (obsevers != null) {
				for (Obsever o : obsevers) {
					o.update();
				}
			}
		}
	}
}
